package com.example.application.views.util;

import com.example.application.bean.PageBean;
import com.example.application.bean.ResultBean;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class ShowAllInfosUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    public static ResultBean showInfos(String uri, int curPage, int pageSize, Class classes) {

        ResultBean resultBean =null;


        try (CloseableHttpClient httpClient = HttpClients.createDefault()){

            uri +=  "/" + curPage + "/" + pageSize;

            HttpGet httpGet = new HttpGet(uri);
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

            String json = EntityUtils.toString(httpResponse.getEntity(), "utf-8");

            JavaType type = mapper.getTypeFactory().constructParametricType(PageBean.class, classes);

            JavaType javaType = mapper.getTypeFactory().constructParametricType(ResultBean.class, type);

            resultBean = mapper.readValue(json, javaType);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return resultBean;
    }
}